All Questions
Tagged with cprogramming-practices
36 questions
10votes
6answers
4kviews
Why are people coding "C-style C++"?
In discussions about whether to cast the result of malloc or not when coding C, one common argument is that if you cast the result then you can compile your C code with a C++ compiler. Why would one ...
-1votes
5answers
2kviews
Why are magic numbers bad practice?
Why are magic NUMBERS considered bad practice? For example: memcpy(ResourcesDir+GameDirLen, "/Resources", 11); What is wrong with the 11? Why are magical numbers so bad? Please, my question ...
-4votes
2answers
150views
How to exploit efficiency of C? [closed]
I want to write a program that performs heavy computations and I want it to be as fast as possible, so I choose C to be the language. Nevertheless, I was told that in spite of its simplicity and high ...
1vote
1answer
223views
Alternatives to service locator with opaque pointer in C
I have a project in which one module keeps the state of the target device (things like current command level, but mostly status registers caches). I'm aware that having a global public variable (...
3votes
4answers
3kviews
How to use Macros in Programming to make code faster, efficient and compact
Recently I was going through some of the source-codes of the best competitive programmers in the world. I found out that those people use a template while writing programs, preferably in C++. I have ...
2votes
1answer
791views
Some questions about implementing a preemptive scheduler in C: Context switching and execution time of the dispatcher
I am trying to implement a preemptive scheduler in C, but I have some understanding problems: When the scheduler is called by an interrupt, a context switch may occur. The context switch can only be ...
14votes
8answers
34kviews
Are C strings always null terminated, or does it depend on the platform?
Right now I am working with embedded systems and figuring out ways to implement strings on a microprocessor with no operating system. So far what I am doing is just using the idea of having NULL ...
27votes
1answer
20kviews
Where did "exit(-1)" come from?
I see in a lot of legacy software and bad tutorials on the Internet that recommend using exit(-1), return -1 or similar to represent "abnormal termination". The problem is, in POSIX at least, -1 has ...
12votes
3answers
3kviews
Is it bad to refer to access array elements via pointer arithmetic instead of the [] operator?
I've just started learning to program in C, and to improve my understanding of pointers and arrays, I tried to refer to the elements of an array without creating any pointer at all: for(k1 = 0; k1 &...
22votes
2answers
4kviews
Has variable width types been replaced by fixed types in modern C?
I came across an interesting point today in a review over on Code Review. @Veedrac recommened in this answer that variable size types (e.g. int and long) be replaced with fixed size types like ...
3votes
3answers
1kviews
How to maintain modularity in C?
I am writing C code with multiple modules like LCD display, flash memory, and GSM module etc. The project consists of thousands of lines of code, in different files. The behavior of the system can be ...
60votes
2answers
93kviews
What is the difference between function() and function(void)?
I have heard that it is a good practice to write functions that do not receive anything as a parameter like this: int func(void); But I hear that the right way to express that is like this: int func(...
3votes
1answer
3kviews
C Module - Where to put prototypes and definitions that do not belong to the public interface?
Since I am mainly an Electrical Engineer, forgive me if I am somewhat off with some terminology. I am currently programming a display driver/interface module. And like so many times before, I have ...
0votes
1answer
208views
Has pre-increment operators become that common? [closed]
Bit of background to explain the reasoning: I've been programming for a good while, but took a break between 2012 and 2014 for other stuff. Before that break, I would hardly ever hear about "++i", let ...
1vote
5answers
319views
Does it make sense to have a separate init function?
I'm created an opaque type and I have two options for its management. The first looks like this: type_t *a = type_init(); int err; err = type_do(a, "foo", "bar", FLAGBIT1|FLAGBIT2, NULL); if (!err) ...